package rs.etf.es.visualization;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JList;

import rs.etf.es.algorithm.Node;
import rs.etf.es.algorithm.NodeAddressBook;

public class VariableSelectedClickHandler extends MouseAdapter {

    private DefaultListModel<String> clickingModel;
    private DefaultListModel<String> toDispalyModel;
    private JList<String> clickingList;

    public VariableSelectedClickHandler(DefaultListModel<String> clickingModel, JList<String> clickingList, DefaultListModel<String> toDisplayModel) {
	this.clickingList = clickingList;
	this.toDispalyModel = toDisplayModel;
	this.clickingModel = clickingModel;
    }

    public void addClickHandler() {
	clickingList.addMouseListener(this);
    }

    public void mouseClicked(MouseEvent event) {
	Node selectedVariable = getClickedNode();
	toDispalyModel.clear();
	for (String value : selectedVariable.getValues()) {
	    toDispalyModel.add(0, value);
	}
    }

    private Node getClickedNode() {
	int index = clickingList.getSelectedIndex();
	String nodeName = clickingModel.getElementAt(index);
	return NodeAddressBook.instance().getNodeForName(nodeName);
    }

}
